package network.TCP.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zxl
 * @version 1.0
 * @date 2021/10/22 10:11
 * 文件接收服务端
 */
public class FileServer {
    public static void main(String[] args) throws Exception {
        //创建连接
        ServerSocket serverSocket = new ServerSocket(9001);
        //获得客户端的连接
        Socket socket = serverSocket.accept();
        //获得输入流
        InputStream inputStream = socket.getInputStream();
        //创建接收文件
        FileOutputStream fileOutputStream = new FileOutputStream(new File("b.jpg"));
        int len;
        byte[] bytes = new byte[1024];
        while ((len=inputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,len);
        }

        //通知客户端已经接收完毕
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("接收完毕".getBytes());

        outputStream.close();
        fileOutputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();

    }
}
